package com.chaodriod.flyviewpager.indicator

import android.content.Context
import android.graphics.drawable.Drawable
import android.support.annotation.ColorInt
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.TextView
import com.chaodriod.flyviewpager.R
import com.chaodriod.flyviewpager.indicator.adapter.IndicatorAdapter
import com.chaodriod.flyviewpager.indicator.listener.IndicatorSelectListener


/**
 * Created by CHEN on 2018-5-6.
 */
class TextTabViewPager : IndicatorViewPager {

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    constructor(context: Context?, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun getLayout(): Int {
        return R.layout.fvp_tab_indicator_viewpager
    }

    override fun getViewPager(): ViewPager {
        return findViewById(R.id.viewpager)
    }

    private val mContainer by lazy {
        findViewById<LinearLayout>(R.id.container)
    }

    private val mScrollView by lazy {
        findViewById<HorizontalScrollView>(R.id.scrollView)
    }

    override fun onAttachedToWindow() {
        mIndicator.init(mContainer)
        super.onAttachedToWindow()
    }

    @ColorInt
    private var mBgColor: Int = ContextCompat.getColor(context, R.color.tab_text_bg)
    @ColorInt
    var mSelectTextColor: Int = ContextCompat.getColor(context, R.color.tab_text_select)
    @ColorInt
    var mUnselectTextColor: Int = ContextCompat.getColor(context, R.color.tab_text_unselect)
    var mSelectTextSize: Float = 36f
    var mUnselectTextSize: Float = 30f
    var mSelectLine: Drawable? = ContextCompat.getDrawable(context, R.drawable.fvp_tab_text_select_line)

    private val mAdapter by lazy {
        TextTabIndicatorAdapter()
    }

    private val mListener by lazy {
        TextTabIndicatorSelectListener()
    }

    override fun getIndicatorView(): IndicatorView {
        return IndicatorView(mViewPager, mAdapter, mListener)
    }

    inner class TextTabIndicatorAdapter : IndicatorAdapter {
        override fun getIndicatorView(position: Int): View {
            val text = TextView(context)
            text.setBackgroundColor(mBgColor)
            text.gravity = Gravity.CENTER
//            LayoutUtils.setMargins(text, 100, 100, 100, 100)
            val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            layoutParams.setMargins(10, 10, 10, 10)//4个参数按顺序分别是左上右下
            text.layoutParams = layoutParams
            text.text = mViewPager.adapter?.getPageTitle(position)
            text.setTextColor(mUnselectTextColor)
            text.setTextSize(TypedValue.COMPLEX_UNIT_PX,mUnselectTextSize)
            mScrollView.requestLayout()
            return text
        }
    }

    inner class TextTabIndicatorSelectListener : IndicatorSelectListener {
        override fun onSelected(select: Int, preSelect: Int) {
            val selectView = getIndicatorItemView(select) as TextView
            val unselectView = getIndicatorItemView(preSelect) as TextView
            selectView.setTextColor(mSelectTextColor)
            unselectView.setTextColor(mUnselectTextColor)
            selectView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mSelectTextSize)
            unselectView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mUnselectTextSize)
            selectView.setCompoundDrawables(null, null, null, mSelectLine)
            unselectView.setCompoundDrawables(null, null, null, null)
            mScrollView.requestLayout()
        }
    }

    fun setIndicatorBg(@ColorInt color : Int){
        mBgColor = color
        mScrollView.setBackgroundColor(color)
    }
}